<?php
require_once(USERS_PATH.'users_app_model.php');

class CmsUser extends UsersAppModel
{
	var $name = 'User';
	
	/**
	 * True means email verification is enable, false means users can signup and 
	 * access their account quickly.
	 */
	var $emailVerification = true;
	
	/**
	 * If set to true, no one can finish signup to the website unless admin approves them
	 */
	var $adminVerification = false;
	var $passwordLength  = 6;
	
	var $actsAs = array(
		'Cms.Metric' => array(
			array('metric' => 'NumRecords')
		)
	);
	
	var $validate = 
	array(
		'username' => array(
			'valid' => array(
				'rule' => VALID_NOT_EMPTY,
				'message' => 'Enter a valid username'
			),
			'duplicate' => array(
				'rule' => array('unique'),
				'required' => VALID_NOT_EMPTY,
				'message' => 'This username is already taken'
			),
			'minLength'=>array(
				'rule'=>array('minLength','6'),
				'message'=>'Username length should be equal or greater than 6'),
		),
		'email' => array(
			'valid' => array(
				'rule' => VALID_EMAIL,
				'required' => VALID_NOT_EMPTY,
				'message' => 'Enter a valid email'
			),
			'duplicate' => array(
				'rule' => array('unique'),
				'message' => 'This email already exists in our system'
			)
		),
		'password' => array(
			'valid' => array(				
				'rule' => VALID_NOT_EMPTY,
				'message' => 'password could not be empty'
			),
			'duplicate' => array(
				'rule' => array('validatePassword'),
				'message' => 'Password not matched with the confirmation.'
			)
		),
		'confirm_password' =>array(
					'rule'=>VALID_NOT_EMPTY,
					'message'=>'Confirm password could not be empty'
				),
		'country' => VALID_NOT_EMPTY,
		'city'	  =>VALID_NOT_EMPTY,	
		'agree'		=> array('rule'=>array('agreeCondition'),'message'=>'You have to agree to term and condition'),
	);

	var $additionalFields = array('full_name','profile_url');
	
	var $useCaptcha = true;
	
	function full_name($user)
	{
		if(isset($user[$this->alias]['username']))
			return $user[$this->alias]['username'];
		elseif(isset($user[$this->alias]['first_name']) && isset($user[$this->alias]['last_name']))
			return $user[$this->alias]['first_name']." ".$user[$this->alias]['last_name'];
		elseif(isset($user[$this->alias]['name']))
			return $user[$this->alias]['name'];
	}


	function validatePostal()
	{
		$zip_exist = $this->query("SELECT COUNT(*) AS total FROM zip_code WHERE zip_code =  '".$this->data[$this->alias]['postal_code']."'");
		if($zip_exist[0][0]['total']>=1)
		$valid = true;
		else
		$valid = false;
		return $valid;
	}

	function validatePassword()
	{
		uses('Security');
		$password =$this->data[$this->alias]['password'];
        $valid = false;
		if($password == Security::hash($this->data[$this->alias]['confirm_password'], null, true))
		{
			$valid = true;
		}
		if((strlen($this->data[$this->alias]['confirm_password'])) < $this->passwordLength)
		{
			$valid = false;
			return $this->validationErrors['password'] = sprintf(__('Password length should be equal or greater than %s'),$this->passwordLength);			
		}	
		return $valid;
	}
	
	function agreeCondition()
	{
		if($this->data[$this->alias]['agree'] >0)
			return true;
		return false; 
	}
	
	function profile_url($user)
	{
		if(!isset($user[$this->alias]['id']))
			return;
		$userId = $user[$this->alias]['id'];
		$username  = "";
		if(isset($user[$this->alias]['username']))
		{
			$username  = Inflector::slug($user[$this->alias]['username'], '-');
		}
		$url = strtolower(Inflector::pluralize($this->alias));
		
		$url = "/$url/profile/$userId/$username";
		return $url;
	}

	function isAuthorized($user, $controller, $action) {

		return true;
		switch ($action)
		{

			case 'default':
				return false;
				break;

			case 'delete':
				if($user['Member']['role'] == 'admin') {
					return true;
				}

				break;
		}

	}

	var $fieldProperties = array(
		'username' => array(
			'group'=>array('signup', 'admin_view')
			
		),
		'email' => array(
			'group'=>array('signup', 'contact', 'grid', 'admin_view'),
		),
		'password' => array(
			'group'=>'signup',
		),
		'confirm_password' => array(
			'group'=>'signup',
			'type' => 'password'
		),		
		'address' => array(
			'group'=>array( 'general', 'admin_view'),
		),
		'city' => array(
			'group'=>array('general', 'profile_view', 'admin_view'),
		),
		'state' => array(
			'group'=>array('general', 'profile_view', 'admin_view'),
			
		),
		'country' => array(
			'group'=>array( 'general', 'profile_view', 'grid', 'admin_view'),
			'type' => 'countrySuggestor',
		),		
		'postal_code' => array(
			'group'=>array( 'general', 'profile_view', 'admin_view'),
			'class' => 'smallInput'
		),
		'gender'=> array(
			'group'=> 'general',
			'type' => 'select',
			'options' => array('Male', 'Female')
		),
		'birthday'=> array(
			'group'=>'general',
			'type' => 'datePicker',
		),				
		
		'emailVerified' => array(
			'options' => array('Not Done', 'Done')
		)
										
	);
	
	function initialize()
	{
		if($this->useCaptcha)
		{
			$this->setFieldProperties('captcha' , array('group'=>'signup','type' => 'captcha'));
		}
		parent::initialize();
	}


}
?>